package de.cbm;

public class LuhnAlgorithmus {

	public static int quersumme(int a) {
		int qs = 0;
		while (a > 0) {
			qs += a % 10;
			a /= 10;
		}
		return qs;
	}

	public static int luhnAlgorithmus(String nummer) {
		int pz = 0;
		int temp;
		for (int i = 0; i < nummer.length()-1; i++) {
			temp = Integer.parseInt(nummer.substring(i, i + 1));
			// Schritt 1,2(, 4)
			if (i % 2 == 0) {
				pz += quersumme(temp *= 2);
			}
			// Schritt 3(, 4)
			else
				pz += temp;
		}
		// Schritt 5
		return ((pz / 10) * 10 + 10) - pz;
	}

	public static boolean isCreditCardValid(String nummer) {
		return Integer.parseInt(nummer.substring(15,16)) ==
			luhnAlgorithmus(nummer) ? true : false;

	}

	public static void main(String[] args) {
		String nr = "2718281828458567";
		if(isCreditCardValid(nr))
			System.out.println("Ok");
		else
			System.out.println("Nicht Ok");
		
	}

}
